/*****************************************************************************/
/*             Perot Systems Framework                                       */
/*****************************************************************************/
/* Stored Procedure :: SetApplicationContext()                               */
/* Set Context for the application                                           */
/*****************************************************************************/
/* Copyright (C) 2003, Perot Systems Corporation. All right reserved.        */
/* Developer: Dmitry Frenkel (dima.frenkel@ps.net)                           */
/* Started:  08/04/2004                                                      */
/*****************************************************************************/
/* Modifications                                                             */
/* Date       |  Author          | Description                               */
/*------------+------------------+-------------------------------------------*/
/*            |                  |                                           */
/*****************************************************************************/

/******************************************************************************/
/* Step 0. Preparations/Declarations                                          */
/******************************************************************************/
SET NOCOUNT OFF

/******************************************************************************/
/* Step 1. Remove stored procedure if it already exists                       */
/******************************************************************************/
IF (SELECT OBJECT_ID('[DBO].[SetApplicationContext]')) IS NOT NULL
BEGIN
  DROP PROCEDURE [DBO].SetApplicationContext
END

GO

/*****************************************************************************/
/* Stored Procedure :: SetApplicationContext()                               */
/*****************************************************************************/
/* Copyright (C) 2003, Perot Systems Corporation. All right reserved.        */
/* Developer: Dmitry Frenkel (dima.frenkel@ps.net)                           */
/* Started:  08/04/2004                                                      */
/*****************************************************************************/
CREATE PROCEDURE [DBO].SetApplicationContext( @ApplicationID BIGINT, 
                                              @Key VARCHAR(64),
                                              @Value VARCHAR(64) )
AS  
BEGIN
	IF NOT EXISTS (SELECT ApplicationID FROM ApplicationContexts WHERE ApplicationID = @ApplicationID AND [Key] = @Key)
	BEGIN
	    INSERT INTO ApplicationContexts( ApplicationID, [Key], [Value] )
	    VALUES( @ApplicationID, @Key, @Value )
	END
	ELSE BEGIN
	    UPDATE ApplicationContexts
	       SET [Value] = @Value
	     WHERE ApplicationID = @ApplicationID AND [Key] = @Key
	END
END 
GO

/******************************************************************************/
/* Step 3. Assign appropriate permissions                                     */
/******************************************************************************/
GRANT EXECUTE ON [DBO].SetApplicationContext TO [Liberty_Users]
GO
-- Developers and Admins have permissions to execute through membership    